package org.example.product.controller;
import org.example.product.entity.ResultBody;
import org.example.product.service.UserCommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/UserComment")
@CrossOrigin(origins="*")
public class UserCommentController {
    @Autowired
    private UserCommentService userCommentService;
    @RequestMapping("/addComment")
    public ResultBody addComment(@RequestBody Map<String,Object> map)
    {
        if(map.get("code")==null||map.get("comment")==null||map.get("point")==null)
        {
            return new ResultBody(404,"缺少参数",false);
        }
        String code=map.get("code").toString();//商品编号
        String comment=map.get("comment").toString();//商品评论
        Object object = map.get("point").toString();//商品分数
        Double point;
        if(object instanceof Integer)
        {
            point=((Integer) object).doubleValue();
        }
        else if(object instanceof Double)
        {
            point=(Double) object;
        }
        else if (object instanceof String)
        {
            point=Double.parseDouble((String)object);
        }
        else
            return new ResultBody(404,"缺少参数",false);
        return userCommentService.addComment(code,comment,point);
    }
}
